package pack0924;

import java.util.Arrays;
import java.util.Scanner;

public class Work01 {
    // 1：小明要去买一部手机，他询问了4家店的价格，分别是2800元，2900元，2750元和3100元，显示输出最低价（要求使用数组）
    public static void a() {
        int[] arr = {2800, 2900, 2750, 3100};
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
            }
        }
        System.out.println(min);
    }

    // 2：有一列乱序的字符，‘a’,‘c’,‘u’,‘b’,‘e’,‘p’,‘f’,‘z’，排序并按照英文字母表的逆序输出
    public static void b() {
        char[] arr = {'a', 'c', 'u', 'b', 'e', 'p', 'f', 'z'};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] < arr[j + 1]) {
                    char temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println(arr);
    }

    // 3：编写程序，将10个数34，3，29，63，70，16，85，82，90，93存放于一组数组中，求出这十个数的和，最大值，最小值以及平均值。
    public static void c() {
        int[] arr = {34, 3, 29, 63, 70, 16, 85, 82, 90, 93};
        int max = arr[0], min = arr[0], sum = arr[0];
        for (int i = 1; i < arr.length; i++) {
            sum += arr[i];
            if (max < arr[i]) {
                max = arr[i];
            }
            if (min > arr[i]) {
                min = arr[i];
            }
        }
        System.out.println(sum);
        System.out.println(max);
        System.out.println(min);
        System.out.println(sum / arr.length);
    }

    // 5：输入数组，最大的与第一个元素交换，最小的与最后一个元素交换，输出数组。
    public static void d() {
        Scanner scan = new Scanner(System.in);
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            System.out.print("请给第" + (i + 1) + "个元素赋值：");
            arr[i] = scan.nextInt();
        }
        int max = arr[0], min = arr[0];
        int maxIndex = 0, minIndex = 0;
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
                maxIndex = i;
            }
            if (min > arr[i]) {
                min = arr[i];
                minIndex = i;
            }
        }
        System.out.println(Arrays.toString(arr));
        // 最大值和第一个交换
        int temp = arr[maxIndex];
        arr[maxIndex] = arr[0];
        arr[0] = temp;
        // 最小值和最后一个交换
        temp = arr[minIndex];
        arr[minIndex] = arr[arr.length - 1];
        arr[arr.length - 1] = temp;
        System.out.println(Arrays.toString(arr));
    }

    // 6：输入数组，返回数组中重复最多和重复最少的元素
    public static void e() {
        int[] arr = new int[20];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 6 + 1);
        }
        System.out.println(Arrays.toString(arr));
        // 记录上一个数组中对应元素的次数
        int[] count = new int[20];
        for (int i = 0; i < arr.length; i++) {
            for (int k : arr) {
                if (arr[i] == k) {
                    count[i]++;
                }
            }
        }
        // 找出count数组中的最大值下标和最小值下标
        int max = count[0], min = count[0];
        int maxIndex = 0, minIndex = 0;
        for (int i = 1; i < count.length; i++) {
            if (max < count[i]) {
                max = count[i];
                maxIndex = i;
            }
            if (min > count[i]) {
                min = count[i];
                minIndex = i;
            }
        }
        System.out.println(Arrays.toString(count));
        System.out.println(arr[maxIndex]);
        System.out.println(arr[minIndex]);
    }

    // 7：输入含有10个元素的数组，求出任意2个数之间的最大公约数
    public static void f() {
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 30 + 1);
        }
        System.out.println(Arrays.toString(arr));
        int max = -1;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                int a = arr[i];
                int b = arr[j];
                int c = a > b ? b : a;
                // 找出a和b的最大公约数
                for (int k = c; k > 0; k--) {
                    if (a % k == 0 && b % k == 0) {
                        if (max < k) {
                            max = k;
                        }
                        break;
                    }
                }
            }
        }
        System.out.println(max);
    }

    /*
    将一个给定的整型数组转置输出，
		例如：源数组，1 2 3 4 5 6
		转置之后的数组，
		6 5 4 3 2 1
     */
    public static void g1() {
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 100 + 1);
        }
        System.out.println(Arrays.toString(arr));
        for (int i = arr.length - 1; i > -1; i--) {
            System.out.print(arr[i] + ",");
        }
        System.out.println("\b");
    }

    public static void g2() {
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 100 + 1);
        }
        System.out.println(Arrays.toString(arr));
        int[] arr2 = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            arr2[i] = arr[arr.length - 1 - i];
        }
        arr = arr2;
        System.out.println(Arrays.toString(arr));
    }

    public static void g3() {
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 100 + 1);
        }
        System.out.println(Arrays.toString(arr));
        for (int i = 1; i < arr.length; i++) {
            int number = arr[i];
            System.arraycopy(arr, 0, arr, 1, i);
            arr[0] = number;
        }
        System.out.println(Arrays.toString(arr));
    }

    // 9：定义a,b数组用于存放两组数据，C数组存放a,b相加的结果
    public static void h1() {
        int[] a = {1, 2, 3, 4};
        int[] b = {6, 7, 8, 9};
        int[] c = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            c[i] = a[i] + b[i];
        }
        System.out.println(Arrays.toString(c));
    }

    public static void h2() {
        int lena = (int) (Math.random() * 10 + 5);
        int lenb = (int) (Math.random() * 10 + 5);
        int[] a = new int[lena];
        int[] b = new int[lenb];
        for (int i = 0; i < lena; i++) {
            a[i] = (int) (Math.random() * 100 + 1);
        }
        for (int i = 0; i < lenb; i++) {
            b[i] = (int) (Math.random() * 100 + 1);
        }
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
        int[] c = new int[Math.max(lena, lenb)];
        for (int i = 0; i < c.length; i++) {
            if (i < Math.min(lena, lenb)) {
                c[i] = a[i] + b[i];
            } else {
                if (lena > lenb) {
                    c[i] = a[i];
                } else {
                    c[i] = b[i];
                }
            }
        }
        System.out.println(Arrays.toString(c));
    }

    /*
        10：现在有如下的一个数组：
		int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
		要求将以上数组中值为0的项去掉，将不为0的值存入一个新的数组，生成的新数组为：
		int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}
     */
    public static void i1() {
        int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
        int removeNumber = 0;
        for (int j : oldArr) {
            if (j == 0) {
                removeNumber++;
            }
        }
        int[] newArr = new int[oldArr.length - removeNumber];
        int count = 0;
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] == 0) {
                count++;
                continue;
            }
            newArr[i - count] = oldArr[i];
        }
        System.out.println(Arrays.toString(newArr));
    }

    public static void i2() {
        int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
        int count = 0;
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] == 0) {
                // 向前移动一位
                System.arraycopy(oldArr, i + 1, oldArr, i, oldArr.length - i - 1);
                count++;
                i--;
            }
        }
        int[] newArr = new int[oldArr.length - count];
        System.arraycopy(oldArr, 0, newArr, 0, oldArr.length - count);
        System.out.println(Arrays.toString(newArr));
    }

    // 有一个随机数组，去重
    public static void j() {
        int lena = (int) (Math.random() * 20 + 5);
        int[] array = new int[lena];
        for (int i = 0; i < lena; i++) {
            array[i] = (int) (Math.random() * 10 + 1);
        }
        System.out.println(Arrays.toString(array));
        int count = 0;
        for (int i = 1; i < array.length - count; i++) {
            boolean flag = true;
            for (int j = 0; j < i; j++) {
                if (array[i] == array[j]) {
                    flag = false;
                    break;
                }
            }
            if (!flag) {
                System.arraycopy(array, i + 1, array, i, array.length - i - 1);
                count++;
                i--;
            }
        }
        int[] array2 = new int[array.length - count];
        System.arraycopy(array, 0, array2, 0, array.length - count);
        System.out.println(Arrays.toString(array2));
    }

    // 有一个整型数组，需要将偶数移动到另一个数组中
    public static void k() {
        int lena = (int) (Math.random() * 30 + 5);
        int[] array = new int[lena];
        for (int i = 0; i < lena; i++) {
            array[i] = (int) (Math.random() * 20 + 1);
        }
        System.out.println(Arrays.toString(array));
        int count = 0;
        // 统计偶数个数
        for (int j : array) {
            if (j % 2 == 0) {
                count++;
            }
        }
        int[] oushu = new int[count];
        int index = 0; // 奇数次数
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 == 0) {
                oushu[i - index] = array[i];
            } else {
                index++;
            }
        }
        System.out.println(Arrays.toString(oushu));
    }

    // 14：编写程序，将一个二维数组的行列互换。（二维数组）
    public static void l() {
        int[][] array = {{1, 2, 3, 4}, {2, 3, 4, 5}, {5, 2, 5, 6}, {9, 8, 7, 6}};
        System.out.println(Arrays.deepToString(array));
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = array[j][i];
            }
        }
        System.out.println(Arrays.deepToString(array));
    }

    public static void main(String[] args) {
        l();
    }
}
/*
1. 复盘原来的知识点：变量、运算符、分支结构、循环结构、数组
2. 练习在做一下
3. 提前预习面向对象知识：Java 类和对象
4. 练习题
 */